//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// State province manager
    /// </summary>
    public class StateProvinceManager
    {
        #region Constants
        private const string STATEPROVINCES_ALL_KEY = "Nop.stateprovince.all-{0}";
        private const string STATEPROVINCES_BY_ID_KEY = "Nop.stateprovince.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Deletes a state/province
        /// </summary>
        /// <param name="StateProvinceID">The state/province identifier</param>
        public static void DeleteStateProvince(int StateProvinceID)
        {
            StateProvinceDBManager.DeleteStateProvince(StateProvinceID);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(STATEPROVINCES_ALL_KEY);
                NopCache.RemoveByPattern(STATEPROVINCES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets a state/province
        /// </summary>
        /// <param name="StateProvinceID">The state/province identifier</param>
        /// <returns>State/province</returns>
        public static StateProvince GetByStateProvinceID(int StateProvinceID)
        {
            string key = string.Format(STATEPROVINCES_BY_ID_KEY, StateProvinceID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (StateProvince)obj2;
            }

            StateProvince stateProvince = StateProvinceDBManager.GetByStateProvinceID(StateProvinceID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, stateProvince);
            }
            return stateProvince;
        }

        /// <summary>
        /// Gets a state/province collection by country identifier
        /// </summary>
        /// <param name="CountryID">Country identifier</param>
        /// <returns>State/province collection</returns>
        public static StateProvinceCollection GetAllByCountryID(int CountryID)
        {
            string key = string.Format(STATEPROVINCES_ALL_KEY, CountryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (StateProvinceCollection)obj2;
            }

            StateProvinceCollection stateProvinceCollection = StateProvinceDBManager.GetAllByCountryID(CountryID);
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, stateProvinceCollection);
            }
            return stateProvinceCollection;
        }

        /// <summary>
        /// Inserts a state/province
        /// </summary>
        /// <param name="CountryID">The country identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Abbreviation">The abbreviation</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>State/province</returns>
        public static StateProvince InsertStateProvince(int CountryID, string Name, string Abbreviation, int DisplayOrder)
        {
            StateProvince stateProvince = StateProvinceDBManager.InsertStateProvince(CountryID, Name, Abbreviation, DisplayOrder);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(STATEPROVINCES_ALL_KEY);
                NopCache.RemoveByPattern(STATEPROVINCES_BY_ID_KEY);
            }

            return stateProvince;
        }

        /// <summary>
        /// Updates a state/province
        /// </summary>
        /// <param name="StateProvinceID">The state/province identifier</param>
        /// <param name="CountryID">The country identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Abbreviation">The abbreviation</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>State/province</returns>
        public static StateProvince UpdateStateProvince(int StateProvinceID, int CountryID, string Name, string Abbreviation, int DisplayOrder)
        {
            StateProvince stateProvince = StateProvinceDBManager.UpdateStateProvince(StateProvinceID, CountryID, Name, Abbreviation, DisplayOrder);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(STATEPROVINCES_ALL_KEY);
                NopCache.RemoveByPattern(STATEPROVINCES_BY_ID_KEY);
            }

            return stateProvince;
        }
        #endregion
    }
}
